/*
Copyright (c) 2009-2010, Warwick Warp Limited
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the Warwick Warp Limited nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL WARWICK WARP LIMITED BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef WARRAY_WARRAY_ND_H_INCLUDED
#define WARRAY_WARRAY_ND_H_INCLUDED

#include <algorithm>
#include <boost/shared_ptr.hpp>

#include "warray_base.h"
#include "warray_accessor.h"
#include "warray_extents.h"
#include "warray_storage.h"

namespace warp {

template<typename T, detail::warray_types::size_type NUM_DIMS, typename Tptr>
class const_arraynd : public array_base<T>
{
protected:
	typedef array_base<T>            super_type;
	typedef array_storage<T>         storage_type;
	typedef detail::const_array_accessor<T,NUM_DIMS,Tptr> accessor_type;
	
	template<detail::warray_types::size_type NUM_INDICES>
	struct const_reference_N
	{
		typedef typename detail::const_accessor_result_type_gen<T,NUM_DIMS,NUM_INDICES,Tptr>::type type;
	};
	
public:
	typedef array_extents<NUM_DIMS>             extents_type;
	typedef array_strides<NUM_DIMS>             strides_type;
	typedef typename super_type::value_type     value_type;
	typedef typename super_type::size_type      size_type;
	typedef typename super_type::index_type     index_type;
	typedef typename const_reference_N<1>::type const_reference;
	typedef const T* const_iterator;
	
	explicit const_arraynd() :
		m_origin(NULL),
		m_size(0)
	{
	}
	
	explicit const_arraynd(const extents_type& extents, const T& fill) :
		m_store(new storage_type(extents.product())),
		m_origin(m_store->store()),
		m_size(extents.product()),
		m_strides(extents),
		m_extents(extents)
	{
		std::fill(m_store->store(),m_store->store()+m_size,fill);
	}
	
	explicit const_arraynd(const extents_type& extents, Tptr carray_) :
		m_origin(carray_),
		m_size(extents.product()),
		m_strides(extents),
		m_extents(extents)
	{
	}
	
	template<typename Optr>
	const_arraynd(const const_arraynd<T,NUM_DIMS,Optr>& other) :
		m_store(other.m_store),
		m_origin(other.m_origin),
		m_size(other.m_size),
		m_strides(other.m_strides),
		m_extents(other.m_extents)
	{
	}
	
	~const_arraynd()
	{
	}
	
	template<typename Optr>
	const_arraynd& operator=(const const_arraynd<T,NUM_DIMS,Optr>& other)
	{
		m_origin  = other.m_origin;
		m_size    = other.m_size;
		m_store   = other.m_store;
		m_strides = other.m_strides;
		m_extents = other.m_extents;
		return *this;
	}
	
	const_iterator begin() const 
	{ 
		return m_origin; 
	}
	
	const_iterator end() const 
	{ 
		return m_origin+m_size; 
	}
	
	size_type size() const
	{ 
		return m_size; 
	}
	
	size_type dim(index_type i) const
	{
		return m_extents.dim(i);
	}
	
	const extents_type& dims() const
	{
		return m_extents;
	}
	
	size_type stride(index_type i) const
	{
		return m_strides.stride(i);
	}
	
	const strides_type& strides() const
	{
		return m_strides;
	}
	
	const_reference operator[](index_type index) const
	{
		accessor_type accessor(m_strides.begin(),m_extents.begin(),m_origin);
		return accessor[index];
	}
	
	template<size_type N>
	typename const_reference_N<N>::type operator[](const array_indices<N>& index) const
	{
		accessor_type accessor(m_strides.begin(),m_extents.begin(),m_origin);
		return accessor[index];
	}
	
	Tptr c_array() const
	{
		return m_origin;
	}
	
	arraynd<T,NUM_DIMS> copy() const;
	
	template<size_type N>
	const_arraynd<T,N,Tptr> reshape(const array_extents<N>& new_extents) const
	{
#ifdef BOUNDS_CHECK
		warp_assert_equal(m_extents.product(), new_extents.product());
#endif
		const_arraynd<T,N,Tptr> answer;
		answer.m_store   = m_store;
		answer.m_strides = array_strides<N>(new_extents);
		answer.m_extents = new_extents;
		answer.m_size    = answer.m_extents.product();
		answer.m_origin  = c_array();
		return answer;
	}
	
	template<size_type N>
	const_arraynd<T,NUM_DIMS-N,Tptr> slice(const array_indices<N>& index) const
	{
		const_arraynd<T,NUM_DIMS-N,Tptr> answer;
		answer.m_store   = m_store;
		answer.m_strides = m_strides.slice<NUM_DIMS-N>();
		answer.m_extents = m_extents.slice<NUM_DIMS-N>();
		answer.m_size    = answer.m_extents.product();
		answer.m_origin  = operator[](index).c_array();
		return answer;
	}
	
	const_arraynd<T,NUM_DIMS,Tptr> strip(index_type start_, index_type end_) const
	{
#ifdef BOUNDS_CHECK
		warp_assert_ge(start_,index_type(0));
		warp_assert_le(start_,end_);
		warp_assert_le(size_type(end_),m_extents.dim(0));
#endif
		const_arraynd<T,NUM_DIMS,Tptr> answer;
		answer.m_store   = m_store;
		answer.m_strides = m_strides;
		answer.m_extents = m_extents;
		answer.m_extents.dim(0) = end_-start_;
		answer.m_size    = answer.m_extents.product();
		answer.m_origin  = m_origin+start_*m_strides.stride(0);
		return answer;
	}
	
protected:
	explicit const_arraynd(const extents_type& extents) :
		m_store(new storage_type(extents.product())),
		m_origin(m_store->store()),
		m_size(extents.product()),
		m_strides(extents),
		m_extents(extents)
	{
	}
	
	boost::shared_ptr<storage_type> m_store;
	
	Tptr         m_origin;
	size_type    m_size;
	strides_type m_strides;
	extents_type m_extents;
	
	template<typename OT, size_type ON, typename Optr>
	friend class const_arraynd;
};

template<typename T, detail::warray_types::size_type NUM_DIMS>
class arraynd : public const_arraynd<T,NUM_DIMS,T*>
{
	typedef const_arraynd<T,NUM_DIMS,T*>       super_type;
	typedef detail::array_accessor<T,NUM_DIMS> accessor_type;
	
	template<detail::warray_types::size_type NUM_INDICES>
	struct reference_N
	{
		typedef typename detail::accessor_result_type_gen<T,NUM_DIMS,NUM_INDICES>::type type;
	};
	
public:
	typedef typename super_type::value_type      value_type;
	typedef typename super_type::size_type       size_type;
	typedef typename super_type::index_type      index_type;
	typedef typename super_type::const_reference const_reference;
	typedef typename super_type::const_iterator  const_iterator;
	typedef typename super_type::extents_type    extents_type;
	typedef typename reference_N<1>::type        reference;
	typedef T*                                   iterator; 
	
	explicit arraynd() :
		super_type()
	{
	}
	
	explicit arraynd(const extents_type& extents) :
		super_type(extents)
	{
	}
	
	explicit arraynd(const extents_type& extents, const T& fillv) :
		super_type(extents,fillv)
	{
	}
	
	explicit arraynd(const extents_type& extents, T *carray_) :
		super_type(extents,carray_)
	{
	}
	
	// We allow implicit conversion from our base class
	arraynd(const super_type& base) :
		super_type(base)
	{
	}
	
	iterator begin() const 
	{ 
		return super_type::m_origin; 
	}
	
	iterator end() const 
	{ 
		return super_type::m_origin+super_type::m_size; 
	}
	
	reference operator[](index_type index) const
	{
		accessor_type accessor(
			super_type::m_strides.begin(),
			super_type::m_extents.begin(),
			super_type::m_origin);
		return accessor[index];
	}
	
	template<size_type N>
	typename reference_N<N>::type operator[](const array_indices<N>& index) const
	{
		accessor_type accessor(
			super_type::m_strides.begin(),
			super_type::m_extents.begin(),
			super_type::m_origin);
		return accessor[index];
	}
	
	void fill(const T& val) const
	{
		std::fill(begin(),end(),val);
	}
	
	template<typename Optr>
	void inject(const const_arraynd<T,NUM_DIMS,Optr>& other) const
	{
		std::copy(other.begin(),other.end(),begin());
	}
};

template<typename T, detail::warray_types::size_type NUM_DIMS, typename Tptr>
arraynd<T,NUM_DIMS> const_arraynd<T,NUM_DIMS,Tptr>::copy() const
{
	arraynd<T,NUM_DIMS> array(m_extents);
	array.inject(*this);
	return array;
}

} // namespace

#endif
